/**
 * 菜单对象
 * @type object
 * @since 1.0 <2015-6-19> SoChishun Added.
 */
var SuNavMenu = {
    newInstance: function () {

        var obj = {
            /**
             * 配置选项
             */
            config: {
                db_name: 'nav_menu',
                sign: '',
                url: '',
                after_reload_fn: function (menus) {
                },
                menu_click_fn: function ($a) {
                },
            },
            /**
             * 设置配置选项
             * @since 1.0 <2015-6-19> SoChishun Added.
             */
            set_config: function (options) {
                $.extend(this.config, options);
            },
            /**
             * 初始化
             * @since 1.0 <2015-6-19> SoChishun Added.
             */
            init: function () {
                this.load_menu();
            },
            /**
             * 加载菜单
             * @since 1.0 <2015-6-19> SoChishun Added.
             */
            load_menu: function () {
                // sign=sha1(rule);
                // {sign:"",menus:[{id:"",name:"",url:""}...]}
                var txt = window.localStorage.getItem(this.config.db_name);
                if (!txt) {
                    this.ajax_load_menu();
                } else {
                    var omenu = JSON.parse(txt);
                    if (this.config.menu_sign !== omenu.sign) {
                        // 签名不一致,重新加载菜单
                        this.ajax_load_menu();
                    } else {
                        this.build_menu(omenu.menus);
                    }
                }
            },
            /**
             * 远程加载菜单
             * @since 1.0 <2015-6-19> SoChishun Added.
             */
            ajax_load_menu: function () {
                $.ajax({
                    method: 'POST',
                    dataType: 'JSON',
                    url: this.config.url,
                    success: function (data) {
                        // {status:true,info:[{id:"",name:"",url:""}...]}
                        if (data.status) {
                            window.localStorage.setItem(obj.config.db_name, JSON.stringify({"sign": obj.config.sign, "menus": data.info}));
                            obj.build_menu(data.info); // 加载成功,重新刷新菜单
                        } else {
                            alert(data.info);
                        }
                    },
                    error: function () {
                        alert('菜单数据加载失败!')
                    },
                });
            },
            /**
             * 生成菜单,可重构
             * @param Array menus
             * @since 1.0 <2015-6-19> SoChishun Added.
             */
            build_menu: function (menus) {
                if (!menus) {
                    alert('没有菜单数据!');
                    return;
                }
                
                var submm = '';
                $(menus).each(function () {
                    submm += '<div id="nav_sub_' + this.id + '" class="main-nav-sub">';
                    $(this.children).each(function () {
                        $(this.children).each(function () {
                            submm += '<div><a href="/index.php/XCall_5_0' + this.url.substring(16) + '" data-id="' + this.id + '">' + this.name + '</a></div>';
                        })
                    })
                    submm += '</div>';
                })
                $(submm).menu({onClick: function () {
                        obj.config.menu_click_fn($(this).find('a'));
                    }});
                $(menus).each(function () {
                    $('#nav_' + this.id).menubutton({menu: '#nav_sub_' + this.id});
                })
                this.config.after_reload_fn(menus); // 生成菜单成功,执行后续操作,如重建导航Tab
            },
        };
        return obj;
    }
};